package com.cskaoyan.service.wx;

import com.cskaoyan.bean.*;
import com.cskaoyan.mapper.MarketCollectMapper;
import com.cskaoyan.mapper.MarketGoodsMapper;
import com.cskaoyan.model.admin.vo.BaseData;
import com.cskaoyan.model.vx.bo.CollectAddordelete;
import com.cskaoyan.model.vx.vo.CollectListByChenVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: ChenZhuqi
 * @Date: 2021/12/3 16:22
 * @Description:
 */
@Service
public class WxCollectByChenServiceImpl implements WxCollectByChenService {
    @Autowired
    MarketCollectMapper collectMapper;
    @Autowired
    MarketGoodsMapper goodsMapper;

    @Override
    //valueId为商品id
    public int Addordelete(CollectAddordelete collectAddordelete, HttpSession session) {
        //得到用户id
        Integer userId = (Integer) session.getAttribute("wxUserId");
        //判断是否登录
        if (userId == null) {
            return 0;
        }
        //之前版本
      /* Integer valueId = collectAddordelete.getValueId();
        MarketCollect marketCollect = new MarketCollect();
        MarketCollectExample collectExample = new MarketCollectExample();
        collectExample.createCriteria().andValueIdEqualTo(valueId).andUserIdEqualTo(userId);
        //先查询出来是否已经收藏过了，
        List<MarketCollect> marketCollects = collectMapper.selectByExample(collectExample);
        // 如果收藏，在数据库中删除，
        if (marketCollects!=null){
            marketCollect.setDeleted(true);
            marketCollect.setUpdateTime(new Date());
            collectMapper.updateByExampleSelective(marketCollect,collectExample);
        }else {
            // 如果未收藏，在数据库中增加
            marketCollect.setUpdateTime(new Date());
            marketCollect.setAddTime(new Date());
            collectMapper.insertSelective(marketCollect);
        }*/
        //借鉴版本
       /* // 查找数据库
        MarketCollectExample collectExample = new MarketCollectExample();
        // 判断valueID
        Integer valueId = collectAddordelete.getValueId();
        //判断type，
        byte type = collectAddordelete.getType();
        collectExample.createCriteria().andUserIdEqualTo(userId).andTypeEqualTo(type).andValueIdEqualTo(valueId);
        long count = collectMapper.countByExample(collectExample);

        MarketCollect marketCollect = new MarketCollect();
        if (count == 0) {//说明没有收藏，需要插入
            marketCollect.setDeleted(false);
            marketCollect.setAddTime(new Date());
            marketCollect.setType(Byte.parseByte(String.valueOf(collectAddordelete.getType())));
            marketCollect.setUserId(userId);
            marketCollect.setUpdateTime(new Date());
            marketCollect.setValueId(collectAddordelete.getValueId());
            collectMapper.insertSelective(marketCollect);
        } else if (count == 1) {
            //得到该商品字段
            List<MarketCollect> marketCollects = collectMapper.selectByExample(collectExample);
            //有,那就更新,更新只更新deleted和updateTime
            Boolean deleted = marketCollects.get(0).getDeleted();
            marketCollects.get(0).setDeleted(!deleted);
        }*/
        //融合
        // 查找数据库
        MarketCollectExample collectExample = new MarketCollectExample();
        // 判断valueID
        Integer valueId = collectAddordelete.getValueId();
        //判断type，
        byte type = collectAddordelete.getType();
        collectExample.createCriteria().andUserIdEqualTo(userId).andTypeEqualTo(type).andValueIdEqualTo(valueId);
        long count = collectMapper.countByExample(collectExample);
        //找出它的deleted
        List<MarketCollect> marketCollects = collectMapper.selectByExample(collectExample);
        MarketCollect marketCollect = new MarketCollect();
        if (count == 0) {//说明没有收藏，需要插入
            marketCollect.setDeleted(false);
            marketCollect.setAddTime(new Date());
            marketCollect.setType(Byte.parseByte(String.valueOf(collectAddordelete.getType())));
            marketCollect.setUserId(userId);
            marketCollect.setUpdateTime(new Date());
            marketCollect.setValueId(collectAddordelete.getValueId());
            collectMapper.insertSelective(marketCollect);
        } else if (count == 1) {
            Boolean deleted = marketCollects.get(0).getDeleted();
            marketCollect.setDeleted(!deleted);
            marketCollect.setUpdateTime(new Date());
            collectMapper.updateByExampleSelective(marketCollect, collectExample);
        }

        return 1;
    }

    @Override
    //查找MarketGoods、marketcollect，根据userID查找
    public BaseData collectList(Byte type, Integer page, Integer limit, HttpSession session) {
        PageHelper.startPage(page, limit);
        //得到用户id，通过用户id查到商品id，找出商品
        //用户id
        Integer userId = (Integer) session.getAttribute("wxUserId");

        //得到收藏表，方便在收藏表中找出该userId的商品id
        MarketCollectExample collectExample = new MarketCollectExample();
        collectExample.createCriteria().andUserIdEqualTo(userId).andTypeEqualTo((byte) 0);
        List<MarketCollect> marketCollects = collectMapper.selectByExample(collectExample);

        //list,用于接收所需要的响应
        List<CollectListByChenVO> list = new ArrayList<>();


        //这是放入在MarketCollect中的字段
        for (MarketCollect marketCollect : marketCollects) {
            //开始进行取值放入list中的操作，先newCollectListByChenVO，再放入list
            CollectListByChenVO collectList = new CollectListByChenVO();
            //根据收藏表中商品id找出对应的商品
            Integer valueId = marketCollect.getValueId();
            MarketGoods marketGoods = goodsMapper.selectByPrimaryKey(valueId);
            //先set到list中goods表中含有的值，
            collectList.setBrief(marketGoods.getBrief());
            collectList.setValueId(marketGoods.getGoodsSn());
            collectList.setPicUrl(marketGoods.getPicUrl());
            collectList.setName(marketGoods.getName());
            collectList.setRetailPrice(marketGoods.getRetailPrice());
            // 后set收藏表中的值
            collectList.setType(marketCollect.getType());
            collectList.setId(marketCollect.getId());
            //list接收
            if (!marketCollect.getDeleted()){
                list.add(collectList);
            }

        }

        PageInfo info = new PageInfo();
        int pages = info.getPages();
        long total = info.getTotal();

        return BaseData.list(list, (int) total, page, limit, pages);
    }
}
